package com.example.lwj.view;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.lwj.utils.CommonUtls;
import com.example.lwj.zhangtianchengnews.R;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

/**
 * Created by lwj on 2016/11/1.
 */
public class viewPagerindicator extends HorizontalScrollView implements View.OnClickListener, ViewPager.OnPageChangeListener {
    private ViewPager viewpager;
    private LinearLayout lin;
    private SlidingMenu slidingMenu;

    //new 的时候使用
    public viewPagerindicator(Context context) {
        super(context);
        init();
    }

    //布局里面引用时用
    public viewPagerindicator(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public viewPagerindicator(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    //初始化操作
    private void init() {
    }

    //定义一个方法拿到viewpager
    public void setViewpager(ViewPager viewpager, SlidingMenu slidingMenu) {
        this.slidingMenu=slidingMenu;
        this.viewpager = viewpager;
        viewpager.setOnPageChangeListener(this);
        lin = new LinearLayout(getContext());
        PagerAdapter adapter = viewpager.getAdapter();
        for (int i = 0; i < adapter.getCount(); i++) {
            View view1 = CommonUtls.inflate(R.layout.indicator_item);
            //找到控件
            view1.setOnClickListener(this);
            TextView line = (TextView) view1.findViewById(R.id.tv_indicator_line);
            TextView text = (TextView) view1.findViewById(R.id.tv_indicator_title);
            if (i == 0) {
                line.setVisibility(View.VISIBLE);
                text.setTextColor(Color.RED);
            } else {
                line.setVisibility(View.INVISIBLE);
                text.setTextColor(Color.GRAY);
            }
            text.setText(adapter.getPageTitle(i));

            LinearLayout.LayoutParams lay = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            lay.setMargins(CommonUtls.dip_px(5), CommonUtls.dip_px(5), CommonUtls.dip_px(5), CommonUtls.dip_px(5));
            lin.addView(view1, lay);

        }
        this.addView(lin);
    }

    @Override
    public void onClick(View v) {
        for (int i = 0; i < lin.getChildCount(); i++) {
            View view = lin.getChildAt(i);
            TextView tv_indicator_title = (TextView) view.findViewById(R.id.tv_indicator_title);
            TextView tv_indicator_line = (TextView) view.findViewById(R.id.tv_indicator_line);
            if (v == lin.getChildAt(i)) {
                viewpager.setCurrentItem(i);
                tv_indicator_title.setTextColor(Color.RED);
                tv_indicator_line.setVisibility(VISIBLE);
        }else {
            tv_indicator_title.setTextColor(Color.GRAY);
            tv_indicator_line.setVisibility(INVISIBLE);
        }
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (position == 0) {
            slidingMenu.setTouchModeAbove(
                    SlidingMenu.TOUCHMODE_FULLSCREEN);
        } else {
            // 当在其他位置的时候，设置不可以拖拽出来(SlidingMenu.TOUCHMODE_NONE)，或只有在边缘位置才可以拖拽出来TOUCHMODE_MARGIN
            slidingMenu.setTouchModeAbove(
                    SlidingMenu.TOUCHMODE_NONE);
        }


        for (int i = 0; i < lin.getChildCount(); i++) {
            View view = lin.getChildAt(i);
            TextView tv_indicator_title = (TextView) view.findViewById(R.id.tv_indicator_title);
            TextView tv_indicator_line = (TextView) view.findViewById(R.id.tv_indicator_line);
            if (i==position) {
                this.scrollTo((int)view.getX()-lin.getChildAt(0).getWidth(),0);
                tv_indicator_title.setTextColor(Color.RED);
                tv_indicator_line.setVisibility(VISIBLE);
            }else {
                tv_indicator_title.setTextColor(Color.GRAY);
                tv_indicator_line.setVisibility(INVISIBLE);
            }
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
